<?php

namespace app\store\controller\wlyunshu;
use app\store\controller\Controllerapi;
use EasyWeChat\Factory;
use think\Request;
use app\common\library\wanshun\Jik ;

/**
 * 门店列表
 * Class Shop
 * @package app\api\controller
 */
class Api extends Controllerapi
{

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // $this->app = $this->createEasyWeChat();
    }
    
    //订单-------------------------------------------------------------------------------------->
    //获取订单数据
    public function getorderlist() {
        //检索订单
        $jik = new Jik();

        $page_index = input('page',0,'intval');
        $page_size = input('pagesize',0,'intval');

        $cCUST_NO = input('cCUST_NO','','string'); //客户名字

        $cLADING_ADDR = input('cLADING_ADDR','','string'); //仓库地址
        $cDESTINATION = input('cDESTINATION','','string'); //终点

        $cCAYODR_DATE = input('cCAYODR_DATE','2024-01-05','string'); //接单日

        $cCUSODR_NO = input('cCUSODR_NO','','string'); //订单号
        $cCUSCAY_NO = input('cCUSCAY_NO','','string'); //运单号

        $cCAY_STATUS = input('cCAY_STATUS/a'); //订单状态

        $filter = [];
        //客户名字
        if(!empty($cCUST_NO)){
            $filter[] = ['cCUST_NO','=',$cCUST_NO];
        }
        //仓库地址
        if(!empty($cLADING_ADDR)){
            $filter[] = ['cLADING_ADDR','instr',$cLADING_ADDR];
        }
        //终点
        if(!empty($cDESTINATION)){
            $filter[] = ['cDESTINATION','instr',$cDESTINATION];
        }
        //订单号
        if(!empty($cCUSODR_NO)){
            $filter[] = ['cCUSODR_NO','instr',$cCUSODR_NO];
        }
        //运单号
        if(!empty($cCUSCAY_NO)){
            $filter[] = ['cCUSCAY_NO','instr',$cCUSCAY_NO];
        }

        $pdata = [
            'filter' => $filter,
            'page_index' => $page_index,
            'page_size' => $page_size,
        ];

        //接单日
        if(!empty($cCAYODR_DATE)){
            $cCAYODR_DATE = str_replace('-', '', $cCAYODR_DATE);
            $pdata['date'] = $cCAYODR_DATE;
        }
        //订单状态
        if(!empty($cCAY_STATUS)){
            $reos =[];
            foreach ($cCAY_STATUS as $os) {
                $reos[] = $os;
            }
            $pdata['cCAY_STATUS'] = $reos;
        }


        // p($pdata);
        // die();

        $list = $jik->ordersearch($pdata);

        $list_group = [];
        $list_group_arr = [];

        if(!empty($list) && isset($list['data'])){
            foreach ($list['data'] as $key => $value) {
                if( !is_null($value['groupid']) ){
                    if(!isset($list_group[$value['groupid']])){
                        $list_group[$value['groupid']] = [];
                    }
                    $list_group[$value['groupid']][] = $value;
                    unset($list['data'][$key]);
                }
            }
        }

        if(!empty($list_group)){
            foreach ($list_group as $key1 => $value1) {
                $list_group_arr[] = ['idx' => $key1,'orderstate'=>"组合单",'sourcenum1'=>count($value1).'单 ('.$key1.')','noedit'=>true];
                foreach ($value1 as $key2 => $value2) {
                    $list_group_arr[] = $value2;
                }
            }
            $list['data'] = array_merge( $list_group_arr,$list['data']) ;
        }
        

        return $this->renderSuccess(compact('list'));
        
    }

    //更新订单
    public function orderset() {

        $idxs = input('idxs','','string');

        $idxs = explode(',',$idxs);

        $pdata = [
            'field' => 'idx',
            'lstdata' => $idxs,
            'dicargs' => ['groupid' => $idxs[0]]
        ];

        //检索订单
        $jik = new Jik();
        
        $rs = $jik->orderset($pdata);

        if($rs){
            return $this->renderSuccess();
        }else{
            return $this->renderError();
        }
        
    }

    //绑定订单跟车辆
    public function order_bind() {

        $idxs = input('idxs','','string');
        $idxs = explode(',',$idxs);

        $groupid = input('groupid',0,'intval');

        $cheids = input('cheids','','string');
        $cheids = explode(',',$cheids);

        //检索订单
        $jik = new Jik();

        $pdata = [
            'field' => 'idx',
            'lstdata' => $idxs,
            'dicargs' => [
                'groupid' => $groupid,
                'orderstate' => '已调度',
            ]
        ];

        $rs = $jik->orderset($pdata);

        $pdata2 = [
            'field' => 'idx',
            'lstdata' => $cheids,
            'dicargs' => [
                'groupid' => $groupid,
                'orderstate' => '已调度',
            ]
        ];

        $rs2 = $jik->setdiaoduche($pdata2);
        /*foreach ($cheids as $key => $value) {

            $pdata2 = [
                'applyId' => $value,
                'dicArgs' => [
                    'groupid' => $groupid,
                    'orderstate' => '已调度',
                ]
            ];
            $rs2 = $jik->setdiaoduche($pdata2);
        }*/

        if($rs){
            return $this->renderSuccess();
        }else{
            return $this->renderError();
        }
    }

    //调度车-------------------------------------------------------------------------------------->
    //获取调度车辆
    public function getdiaoduche() {
        $groupid = input('groupid',0,'intval');
        $status = input('status','','string');

        $page_index = input('page',0,'intval');
        $page_size = input('pagesize',0,'intval');

        $filter = [];
        //分组
        if(!empty($groupid)){
            $filter[] = ['groupid','=', $groupid];
        }
        if(!empty($status)){
            $filter[] = ['orderstate','instr', $status];
        }
        
        //检索订单
        $jik = new Jik();

        $pdata = [
            'filter' => $filter,
            'page_index' => $page_index,
            'page_size' => $page_size,
        ];
        
        $list = $jik->getdiaoduche($pdata);

        return $this->renderSuccess(compact('list'));
        
    }
    //新增调度车辆
    public function adddiaoduche() {

        $data = input('obj/a');
        $pdata = [];
        foreach ($data as $key => $value) {
            if(!empty($value)){
                 $pdata[$key] = $value;
            }
        }

        //检索订单
        $jik = new Jik();

        $rs = $jik->adddiaoduche(['dicargs'=>$pdata]);

        return $this->renderSuccess(compact('list'));
        
    }
    //调度车-------------------------------------------------------------------------------------->



    //承运单-------------------------------------------------------------------------------------->
    //获取承运单
    public function getchengyunorder() {
        $groupid = input('groupid',9999999,'intval');
        $status = input('status','','string');

        $filter = [];
        //分组
        if(!empty($groupid)){
            $filter[] = ['groupid','=', $groupid];
        }
        /*if(!empty($status)){
            $filter[] = ['groupid','=', $groupid];
            $lstFilter[] = [
                'field' =>'调车状态',
                'value' => $status,
            ];
        }*/

        //检索订单
        $jik = new Jik();

        $pdata = [
            'filter' => $filter,
            'page_index' => 1,
            'page_size' => 9999999,
        ];
        
        $list = $jik->getchengyunorder($pdata);

        return $this->renderSuccess(compact('list'));
        
    }
    //新增承运单
    public function addchengyunorder() {

        $data = input('obj/a');
        $pdata = [];
        foreach ($data as $key => $value) {
            if(!empty($value)){
                 $pdata[$key] = $value;
            }
        }

        //检索订单
        $jik = new Jik();

        $rs = $jik->addchengyunorder(['dicargs'=>$pdata]);

        return $this->renderSuccess(compact('list'));
        
    }
    //获取承运单费用
    public function getchengyunorderprice() {
        $groupid = input('groupid',999999999,'intval');

        $filter = [];
        //分组
        if(!empty($groupid)){
            $filter[] = ['groupid','=', $groupid];
        }

        $jik = new Jik();
        $pdata = [
            'filter' => $filter,
            'page_index' => 1,
            'page_size' => 9999999,
        ];
        
        $list = $jik->getchengyunorderprice($pdata);

        return $this->renderSuccess(compact('list'));
        
    }
    //新增承运单费用
    public function addchengyunorderprice() {

        $data = input('obj/a');
        $pdata = [];
        foreach ($data as $key => $value) {
            if(!empty($value)){
                 $pdata[$key] = $value;
            }
        }

        //检索订单
        $jik = new Jik();

        $rs = $jik->addchengyunorderprice(['dicargs'=>$pdata]);

        return $this->renderSuccess(compact('list'));
        
    }




    //承运商分类-------------------------------------------------------------------------------------->
    //获取承运商分类
    public function getchengyunfl() {

        $lstFilter = [];
        //分组
        if(!empty($groupid)){
            $lstFilter[] = [
                'field' =>'groupid',
                'value' => $groupid,
            ];
        }
        if(!empty($status)){
            $lstFilter[] = [
                'field' =>'调车状态',
                'value' => $status,
            ];
        }

        //检索订单
        $jik = new Jik();

        $pdata = [
            'lstFilter' => $lstFilter,
            'page_index' => 1,
            'page_size' => 9999999,
        ];
        
        $list = $jik->getchengyunorder($pdata);

        return $this->renderSuccess(compact('list'));
        
    }
    //获取承运商
    public function getchengyunshang() {
        $no = input('cSUPLKIND_NO',0,'intval') ;

        //检索订单
        $jik = new Jik();

        $pdata = [
            'cSUPLKIND_NO' => $no,
            'page_index' => 1,
            'page_size' => 10,
        ];

        $list = $jik->getchengyunshang($pdata);
        return $this->renderSuccess(compact('list'));
    }


    //绑定订单跟承运单------------------------------------------------------>
    public function order_bind_cyd() {

        $idxs = input('idxs','','string');
        $idxs = explode(',',$idxs);
        $groupid = input('groupid',0,'intval');
        //承运单数据
        $cydarr = input('cydarr/a');
        //费用数据
        $pricearr = input('pricearr/a');

         //检索订单
        $jik = new Jik();

        //承运单存储
        foreach ($cydarr as $key => $value) {
            $cydarr[$key]['orderstate'] = '已调度';
            $cydarr[$key]['groupid'] = intval($groupid);
            unset($cydarr[$key]['idx']) ;
            unset($cydarr[$key]['LAY_NUM']) ;
            unset($cydarr[$key]['LAY_INDEX']) ;

            $jik->addchengyunorder(['dicargs'=>$cydarr[$key]]);
        }
        //承运单费用存储
        foreach ($pricearr as $key => $value) {
            $pricearr[$key]['groupid'] = intval($groupid);
            unset($pricearr[$key]['idx']);
            unset($pricearr[$key]['LAY_NUM']) ;
            unset($pricearr[$key]['LAY_INDEX']) ;
            $jik->addchengyunorderprice(['dicargs'=>$pricearr[$key]]);
        }


        $pdata = [
            'field' => 'idx',
            'lstdata' => $idxs,
            'dicargs' => [
                'groupid' => $groupid,
                'orderstate' => '已调度',
            ]
        ];

        $rs = $jik->orderset($pdata);


        if($rs){
            return $this->renderSuccess();
        }else{
            return $this->renderError();
        }
    }

    
    //通用select 检索api
    public function baseapi() {
        $pagesize = input('pagesize',9999999,'intval') ;
        $page = input('page',1,'intval') ;
        $action = input('action','','string') ;
        $show_value_field = input('show_value_field','','string') ;
        $show_value_label = input('show_value_label','','string') ;
        //全局检索字段
        $extra_where = input('extra_where/a',[]) ;

        $jik = new Jik();

        if($action == 'area'){
            //省级资料
            $pdata = [
                'action' => 'info/prov/search',
                'page_index' => 1,
                'page_size' => 9999999,
            ];
            $prov = array_reverse($jik->baseapi($pdata)['data']) ;

            $pdata = [
                'action' => 'info/city/search',
                'page_index' => 1,
                'page_size' => 9999999,
            ];
            $city = $jik->baseapi($pdata)['data'];

            $city_box = [];
            foreach ($city as $key => $value) {
                if(!isset($city_box[$value['cPROV_NO']])){
                    $city_box[$value['cPROV_NO']] = [];
                }
                $city_box[$value['cPROV_NO']][] = [
                    'cCITY_NO'=>$value['cCITY_NO'],
                    'cCITY_NM'=>$value['cCITY_NM'],
                    'label'=>$value['cCITY_NM']
                ];
            }

            $prov_box = [];
            foreach ($prov as $key1 => $value1) {
                if($value1['cPROV_NO'] != 'CN'){
                    $prov_box[] = [
                        'cPROV_NO'=>$value1['cPROV_NO'],
                        'cPROV_NM'=>$value1['cPROV_NM'],
                        'label'=>$value1['cPROV_NM'],
                        'children' => $city_box[$value1['cPROV_NO']]
                    ];
                }
            }
            
            $list['data'] = $prov_box;
            return $this->renderSuccess(compact('list'));
        }
        
        //检索订单
        
        $pdata = [
            'action' => $action,
            'extra_where' => $extra_where,
            'page_index' => $page,
            'page_size' => $pagesize,
        ];

        $list = $jik->baseapi($pdata);

        if( !empty($list) && !empty($list['data']) && !empty($show_value_field) && !empty($show_value_label) ){
            foreach ($list['data'] as $key => $value) {
                $list['data'][$key]['value'] = $value[$show_value_field] ;
                $list['data'][$key]['label'] = $value[$show_value_label] ;
            }
        }

        return $this->renderSuccess(compact('list'));
    }

    //通用select 检索api
    public function baseseveapi() {
        $params = input('params/a',[]) ;
        $jik = new Jik();
        $rs = $jik->baseseveapi(['dicargs'=>$params]);
        if($rs){
            return $this->renderSuccess();
        }else{
            return $this->renderError();
        }
    }

/*
    //费用类型------------------------------------------>
    public function getpricetype() {

        
        
    }
    
*/


}
